package com.molyfun.parents.activity.signup;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.fragment.dialog.DatePickerDialogFragment;
import com.molyfun.parents.fragment.dialog.SexChooseDialogFragment;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.bean.SignUpDetailResponse;
import com.molyfun.parents.rest.bean.SignUpFixStudentInfoBean;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.utils.IDCardVerify;
import com.molyfun.parents.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SignUpEditeStudentInfoActivity extends BaseActivity implements View.OnClickListener {

    private String TAG = "SignUpEditeStudentInfoActivity";
    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.edite_student_info_submit_button)
    TextView mSubmitButton;
    @InjectView(R.id.student_layout)
    ScrollView mStudentLayout;
    @InjectView(R.id.layout_mother_info)
    LinearLayout mLauoytStudnet;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.sex_choose)
    TextView mSexChoose;
    @InjectView(R.id.base_title)
    TextView mTitle;
    private String student_Id;
    private String studentName;
    private String studentSex;
    private long studentBirthdate;
    private String studentParity;
    private String student_localAddress;
    private String student_identity;
    private String useraddress;
//    private SexCheckView sexCheck;
    private TextView studentBornDate;
    private TextView studentEmbory;
    private TextView studentLocalAddress;
    private EditText studentIdentity;
    private EditText studentFamilyAddress;

    private List<String> parityLists = new ArrayList<String>();
    private ListPopupWindow mListPop;
    private String identity;

    ////0:男 1:女
    private String mSexStr = "男";

    public static void startActivity(Context context, int category, SignUpDetailResponse.DataBean.StudentenrolpageBean studentenrolpage, String useraddress) {
        Intent intent = new Intent();
        intent.putExtra("category", category);
        intent.putExtra("student_Id", studentenrolpage.getId());
        intent.putExtra("student_name", studentenrolpage.getName());
        intent.putExtra("student_sex", studentenrolpage.getSex());
        intent.putExtra("student_birthdate", studentenrolpage.getBirthdatetime());
        intent.putExtra("student_parity", studentenrolpage.getParity());
        intent.putExtra("student_localAddress", studentenrolpage.getCityname());
        intent.putExtra("student_identity", studentenrolpage.getCardid());
        intent.putExtra("useraddress", useraddress);
        intent.setClass(context, SignUpEditeStudentInfoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_edite_student_info);
        ButterKnife.inject(this);

        initView();
    }

    private void initView() {
        getSupportActionBar().setTitle("");
        Intent intent = getIntent();
        mTitle.setText("编辑学生信息");

        mStudentLayout.setVisibility(View.VISIBLE);
        mRecyclerView.setVisibility(View.GONE);
        student_Id = intent.getStringExtra("student_Id");
        studentName = intent.getStringExtra("student_name");
        studentSex = intent.getStringExtra("student_sex");
        studentBirthdate = intent.getLongExtra("student_birthdate", 0);
        studentParity = intent.getStringExtra("student_parity");
        student_localAddress = intent.getStringExtra("student_localAddress");
        student_identity = intent.getStringExtra("student_identity");
        useraddress = intent.getStringExtra("useraddress");
        EditText edite_studentName = (EditText) mLauoytStudnet.findViewById(R.id.student_info_name);
        edite_studentName.setText(studentName);
        //设置姓名不可编辑
        edite_studentName.setEnabled(false);
        edite_studentName.setFocusable(false);
        edite_studentName.setKeyListener(null);//重点
//        sexCheck = (SexCheckView) mLauoytStudnet.findViewById(R.id.sex_check_view);
        studentBornDate = (TextView) mLauoytStudnet.findViewById(R.id.student_info_born_date);
        studentEmbory = (TextView) mLauoytStudnet.findViewById(R.id.student_info_embory);
        studentLocalAddress = (TextView) mLauoytStudnet.findViewById(R.id.student_info_identity_local);
        studentIdentity = (EditText) mLauoytStudnet.findViewById(R.id.student_info_identity_card);
        studentFamilyAddress = (EditText) mLauoytStudnet.findViewById(R.id.reservation_info_home_address);
        LinearLayout layout_reservation_info_email = (LinearLayout) mLauoytStudnet.findViewById(R.id.layout_reservation_info_email);
        LinearLayout layout_edit_student_phone = (LinearLayout) mLauoytStudnet.findViewById(R.id.layout_edit_student_phone);
        LinearLayout layout_phone = (LinearLayout) mLauoytStudnet.findViewById(R.id.layout_phone);
        LinearLayout layout_email = (LinearLayout) mLauoytStudnet.findViewById(R.id.layout_email);
        layout_reservation_info_email.setVisibility(View.GONE);
        layout_edit_student_phone.setVisibility(View.GONE);
        layout_phone.setVisibility(View.GONE);
        layout_email.setVisibility(View.GONE);
        if (studentSex.equals("1")) {
            mSexChoose.setText("女");
            mSexStr = "女";
        } else {
            mSexChoose.setText("男");
            mSexStr = "男";
        }
        studentBornDate.setText(TimeUtils.longToString(studentBirthdate, "yyyy-MM-dd"));
        studentEmbory.setText(studentParity);
        studentLocalAddress.setText(student_localAddress);//
        studentIdentity.setText(student_identity);
        studentFamilyAddress.setText(useraddress);
        studentBornDate.setOnClickListener(this);
        mSexChoose.setOnClickListener(this);


        mBack.setOnClickListener(this);
        studentEmbory.setOnClickListener(this);
        mSubmitButton.setOnClickListener(this);
        studentLocalAddress.setOnClickListener(this);
        selectorParity();
    }

    private void selectorParity() {
        parityLists.add("1");
        parityLists.add("2");
        parityLists.add("3");

        mListPop = new ListPopupWindow(this);
        mListPop.setAdapter(new ArrayAdapter<String>(this, R.layout.layout_parity_pop, parityLists));
        mListPop.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        mListPop.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        mListPop.setAnchorView(studentEmbory);//设置ListPopupWindow的锚点，即关联PopupWindow的显示位置和这个锚点
        mListPop.setModal(true);//设置是否是模式
        mListPop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                studentEmbory.setText(parityLists.get(position));
                mListPop.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.edite_student_info_submit_button:
                submiteInfo();
                break;
            case R.id.student_info_born_date:
                final String currentTime = TimeUtils.longToString(System.currentTimeMillis(), "yyyy-MM-dd");
                TimeUtils.getChooseDate(this, new DatePickerDialogFragment.IDateCallback() {
                    @Override
                    public void onTimeChoosed(String time) {
                        String[] splitTime = time.split("-");
                        String[] currentSplitTime = currentTime.split("-");
                        if (Integer.valueOf(splitTime[0] + splitTime[1] + splitTime[2]) <= Integer.valueOf(currentSplitTime[0] + currentSplitTime[1] + currentSplitTime[2])) {
                            studentBornDate.setText(time);
                        } else {
                            toast("出生日期大于当前日期");
                        }
                    }
                });
                break;
            case R.id.student_info_embory:
                mListPop.show();
                break;
            case R.id.student_info_identity_local:
                //户口所在地

                break;
            case R.id.sex_choose:
                new SexChooseDialogFragment.SexChooseDialogBuilder()
                        .setSexChooseCallback(new SexChooseDialogFragment.SexChooseCallback() {
                            @Override
                            public void onSexChoose(boolean isMale) {
                                if(isMale){
                                    mSexStr = "男";
                                    mSexChoose.setText("男");
                                }else{
                                    mSexStr = "女";
                                    mSexChoose.setText("女");
                                }
                            }
                        })
                        .build(getSupportFragmentManager().beginTransaction());
                break;
        }
    }

    private void submiteInfo() {

        if (!TextUtils.isEmpty(studentIdentity.getText().toString().trim()) && IDCardVerify.verifyIdNumber(studentIdentity.getText().toString().trim())) {
            identity = studentIdentity.getText().toString().trim();
        } else {
            if (TextUtils.isEmpty(studentIdentity.getText().toString().trim())) {
            } else {
                toast("身份证号码有误");
                return;
            }
        }
        SignUpFixStudentInfoBean signUpFixStudentInfoBean = new SignUpFixStudentInfoBean();
        signUpFixStudentInfoBean.setAddress(studentFamilyAddress.getText().toString().trim() + "");
        signUpFixStudentInfoBean.setBirthdatetime(studentBornDate.getText().toString().trim() + "");
        signUpFixStudentInfoBean.setCardid(identity);
        signUpFixStudentInfoBean.setParity(studentEmbory.getText().toString().trim() + "");
        signUpFixStudentInfoBean.setSex(mSexStr);
        signUpFixStudentInfoBean.setId(student_Id);
        showProgressDialog();
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().signUpFixStudentInfo(signUpFixStudentInfoBean);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getMsg() != null) {
                    Message uimessage = new Message();
                    uimessage.what = EventDispatcherEnum.UI_EVENT_SIGNUP_EDITE_STUDENT_INFO;
                    MFPApplication.getInstance().getEventDispatcher().sendMessage(uimessage);
                    finish();
                } else {
                    toast(response.body() == null ? "" : response.body().getMsg() == null ? "" : response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("提交失败");
            }
        });
    }
    
}
